package cn.dnui_zjq823.dnuifood_16110100823.fragment;


import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cn.dnui_zjq823.dnuifood_16110100823.R;

/**
 * Created by dell on 2018/6/12.
 */

public class CollectionFragment extends Fragment{
    private DisplayMetrics dm;
    private PagerSlidingTabStrip pagersliding;
    private ViewPager viewpager;
    private CollectionShopFragment fragment1=null;
    private CollectionFoodFragment fragment2=null;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment__collection,container,false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);;
        dm=getResources().getDisplayMetrics();
        viewpager=view.findViewById(R.id.viewPager);
        viewpager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        pagersliding=view.findViewById(R.id.pagerslidingtabstrip);
        pagersliding.setViewPager(viewpager);
        setpagerstyle();
    }

    private void setpagerstyle() {
        pagersliding.setShouldExpand(true);
        pagersliding.setDividerColor(Color.TRANSPARENT);
        pagersliding.setUnderlineHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        pagersliding.setIndicatorHeight((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, dm));
        pagersliding.setTextSize((int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 16, dm));
        pagersliding.setIndicatorColor(Color.parseColor("#45c01a"));
        pagersliding.setSelectedTextColor(Color.parseColor("#45c01a"));
        pagersliding.setTabBackground(0);

    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        private String []titles={"店铺","菜品"};
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public Fragment getItem(int position) {
            switch (position){
                case 0:
                    if (fragment1==null){
                        fragment1=new CollectionShopFragment();
                    }
                    return fragment1;
                case 1:
                    if (fragment2==null){
                        fragment2=new CollectionFoodFragment();
                    }
                    return fragment2;
                default:
                    return null;
            }
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }
}
